package leetcode.hot100;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class number15 {
    public static void main(String[] args) {
        List<List<Integer>> list = threeSum(new int[]{-1,0,1,2,-1,-4});
        for (List<Integer> integers : list) {
            System.out.println(integers);
        }
    }
    public static List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>>list=new ArrayList<>();
        Arrays.sort(nums);
        for(int i=0;i<nums.length;i++){
            if(i-1>=0&&nums[i-1]==nums[i]){
                continue;
            }
            if(nums[i]>0){
                break;
            }
            for(int j=i+1;j<nums.length-1;j++){
                if(j-1>=i+1&&nums[j-1]==nums[j]){
                    continue;
                }
                int l=nums.length-1;
                while(j<l){
                    int sum=nums[i]+nums[j]+nums[l];
                    if(sum>0) {
                        while(l>j&&nums[l]==nums[l-1]){
                            l--;
                        }
                    }
                    if(sum<0){
                        if(j-1>=i+1&&nums[j-1]==nums[j]){
                            j++;
                        }
                    }
                    if(sum==0) {
                        //list.add(List.of(new Integer[]{nums[i],nums[j],nums[l]}));
                        l--;
                        j++;
                    }
                }

            }
        }
        return list;
    }
}
